package com.ofek.eventos.gui.list_table;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import com.ofek.eventos.db.datatypes.EventData;

public class EventsListTableGUI extends ListTableFrame<EventData> {

	private static EventsListTableGUI instance;
	private Runnable deleteUsers;
	private Runnable deleteEvents;
	private JPanel view;
	private JPanel buttonPanel;
	public EventsListTableGUI(final ArrayList<EventData> table) {
		super("Events");
		final Container contentPane = getContentPane();
		contentPane.add(view = new JPanel());
		view.setLayout(new BorderLayout());
		buttonPanel = new JPanel();
		final JButton deleteEvent = new JButton("Delete Events");
		deleteEvent.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent e) {
				deleteEvents.run();
			}
		});
		
		final JButton deleteUser = new JButton("Delete Users");
		deleteUser.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent e) {
				deleteUsers.run();
			}
		});
		buttonPanel.add(deleteEvent);
		buttonPanel.add(deleteUser);
		
		setTable(new ListTable<>(new ListTableModel<>(table)));
		setPreferredSize(new Dimension(1000,1000));
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	@Override
	public void setTable(ListTable<EventData> table) {
		view.removeAll();
		view.add(buttonPanel,BorderLayout.NORTH);
		super.setTable(table);
		
	}
	public static EventsListTableGUI getInstance() {
		if (instance ==null) instance = new EventsListTableGUI(new ArrayList<EventData>());
		return instance;
	}
	@Override
	protected void onEmpty() {
		view.add(new JLabel("No Data to Display",SwingConstants.CENTER),BorderLayout.CENTER);
		pack();
	}
	@Override
	protected void internalSetTable(final ListTable<EventData> listTable2) {
		view.add(new JScrollPane(listTable2),BorderLayout.CENTER);
		pack();
	}
	 public void onDeleteUsers(Runnable run) {
		 deleteUsers = run;
	 }
	 public void onDeleteEvents(Runnable run) {
		 deleteEvents = run;
	 }

}
